package com.AUI.components
{
	import com.AUI.*;
	import com.AUI.define.style.*;
	
	import flash.display.*;
	import flash.text.*;
	/**
	 * Button.
	 * @author HuangMinPing
	 * @site www.AminLab.com
	 */
	public class Button extends AComponent
	{
		/**
		 * [element] icon.
		 */
		public var e_icon:Shape = new Shape;
		/**
		 * [element] text.
		 */
		public var e_text:TextField = new TextField;
		/**
		 * Constructor.
		 */
		public function Button(label:String = '')
		{
			// init attributes
			depth = 1;
			align = true;
			// init elements
			e_text.mouseEnabled = false;
			e_text.text = label;
			elements.addChild(e_text);
			elements.addChild(e_icon);
			
			/**************** INITCOMPLETE ****************/
			Object(this).constructor == Button && initComplete();
		}
		/**
		 * Get or set the label text of the button.
		 */
		override public function set value(variable:*):void
		{
			var label:String = variable as String;
			e_text.text = label;
			updateContentBox();
		}
		override public function get value():*
		{
			return e_text.text;
		}
		/**
		 * @private
		 */
		override protected function updateDrawStyle():void
		{
			super.updateDrawStyle();
			// update icon
			e_icon.graphics.clear();
			var icon:IMAGE = drawStyle.icon;
			if(icon.image)
			{
				//e_icon.graphics.beginBitmapFill(icon.image.bitmapData,null,icon.repeat);
				//e_icon.graphics.drawRect(BL,BT,PW,PH);
			}
			// update text
			var font:FONT = drawStyle.font; 
			var textFormat:TextFormat = new TextFormat(font.family,font.size,font.color,font.bold,font.italic,font.underline,null,null,font.align);
			e_text.defaultTextFormat = textFormat;
			e_text.setTextFormat(textFormat);
		}
		/**
		 * @private
		 */
		override protected function resizeBoxModel():void
		{
			if(e_text.text)
			{
				e_text.autoSize = TextFieldAutoSize.LEFT;
			}
			else
			{
				e_text.autoSize = TextFieldAutoSize.NONE;
				e_text.width = e_text.height = 0;
			}
			// update placement
			var EV:int;
			var E1:DisplayObject;
			var E2:DisplayObject;
			switch(drawStyle.font.placement)
			{
				case 'right':
					EV = 0; E1 = e_icon; E2 = e_text;
					break;
				case 'left':
					EV = 0; E1 = e_text; E2 = e_icon;
					break;
				case 'bottom':
					EV = 1; E1 = e_icon; E2 = e_text;
					break;
				case 'top':
					EV = 1; E1 = e_text; E2 = e_icon;
					break;
			}
			var EW1:int = E1.width;
			var EH1:int = E1.height;
			var EW2:int = E2.width;
			var EH2:int = E2.height;
			var EG:int;
			if(EV)
			{
				EG   = (EH1 && EH2) ? drawStyle.gap.v : 0;
				E1.y = 0;
				E2.y = EH1 + EG;
			}
			else
			{
				EG   = (EW1 && EW2) ? drawStyle.gap.h : 0;
				E1.x = 0;
				E2.x = EW1 + EG;
			}
			// super do
			super.resizeBoxModel();
		}
	}
}